<?php
namespace Poi\EttsBundle\Model;

/**
 * @author Ricardo J. Vazquez
 * 
 * Representation of a Officer object from the database
 */
class Officer extends Model
{
	public $BadgeNumber = 0;
	public $Name = '';
	public $PoliceUnit = '';
	public $Tickets = null; //Tickets
	
	/**
	* Sets up the Tickets property
	*/
	public function init(){
		if(!empty($this->id)){
			$this->BadgeNumber = $this->id;
		}
				
		$this->Tickets = new Tickets($this->conn, $this->BadgeNumber);
		$this->Tickets->setLoadCallback('loadTicketsOfficer','Ticket',$this->Tickets);
	}

	protected function prepareLoadQuery(){
		if(!empty($this->BadgeNumber)){
			$sql = "SELECT
						O1.BadgeNumber,
						O1.Name,
						O1.PoliceUnit
					FROM Officer O1
					WHERE O1.BadgeNumber = :num";

			$this->loadStmt = $this->conn->prepare($sql);

			$this->loadStmt->bindValue("num", $this->BadgeNumber);
		}
	}
	
	public function prepareUpdateQuery(){
		
	}
	
	public function prepareCreateQuery(){

	}
	
	public function prepareDeleteQuery(){
		
	}
	
	public function toXML(\SimpleXMLElement &$xml=null){
		$return = false;
		if(is_null($xml)){
			$xml = new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?><xmlresponse></xmlresponse>');
			$return = true;
		}
		
		$officerXML = $xml->addChild('officer');
		
		$officerXML->addChild('badgeNumber',$this->getBadgeNumber());
		$officerXML->addChild('name',$this->getName());
		$officerXML->addChild('policeUnit',$this->getPoliceUnit());
		
		$this->getTickets()->toXML($officerXML);
		
		if($return){		
			return $xml->asXML();
		}	
	}
}